
<?php 
$memAccount = $this->Session->read('member_account'); 
$orgAccount = null;

if( $this->Session->check('org_account') ) {
    $orgAccount = $this->Session->read('org_account');
}
?>

<div class='m-t col-lg-12 b-b'>

    <div class="row m-b-sm">
        <div class="col-lg-8">
            <h4 class='font-bold text-info'>Your Organisations</h4>
            <p>Organisations you are currently a member of or manage as an admin</p>
        </div>
        <div class="col-lg-4 text-right">
            <a href="<?php echo $this->Html->url(array('controller' => 'organisation', 'action' => 'create')); ?>" class="m-t btn btn-sm btn-white"><i class='fa fa-plus'></i> Create Organisation</a>
        </div>
    </div>
</div>


<div class="wrapper row">
    <div class="col-lg-12">
        <?php
        if (isset($organisations)) {
            foreach ($organisations as $org) {
                ?>
                <div class="row b-b m-b-sm p-xs org_list_item" data-org-id="<?php echo $org['Organisation']['id']; ?>">
                    <div class="col-lg-1 col-xs-2 col-sm-2 col-md-2 text-center">
                        <?php if ($org['Organisation']['logo']) { ?>
                            <img src="<?php echo $this->Html->url($org['Organisation']['logo']); ?>" class="thumb img-thumbnail">
                        <?php } else { ?>
                            <img src="<?php echo $this->Html->url('/img/logo_placeholder.jpg'); ?>" class="thumb img-thumbnail">
                        <?php } ?>
                    </div>
                    <div class="col-lg-8 col-xs-6 col-sm-7 col-md-7">
                        <p class="font-bold"><?php echo $org['Organisation']['name']; ?></p>
                        <p class="text-muted text-sm m-t-n-sm">
                            Members: <?php echo $org['Organisation']['members']; ?>
                        </p>
                    </div>
                    <div class="col-lg-3 col-xs-4 col-sm-3 col-md-3">
                        <!--nocache-->
                        <?php
                        $slug = $org['Organisation']['slug'];
                        $org_url = $this->Html->url("/member/org_login/$slug");

                        if (isset($org['OrganisationAccount'][0]) && $org['OrganisationAccount'][0]['active'] == 1 ) {
                            if( $orgAccount && $orgAccount['Organisation']['slug'] == $slug ) {
                                $org_url = $this->Html->url("/$slug");
                                echo "<a href='$org_url' class='org_manage btn btn-sm btn-info'>Resume</a>";
                            } 
                            
                            else { 
                                echo "<a href='$org_url' class='org_manage btn btn-sm btn-success' data-toggle='ajaxModal'>Manage</a>";
                            }
                            
                            if( isset($org['OrganisationAccount'][0]['OrganisationRole']['admin_access']) ) {
                                echo " <a href='$org_url' class='org_remove btn btn-sm btn-danger'><i class='fa fa-trash-o'></i></a>";
                            }
                            
                        } else {
                            echo "<a href='" . $this->Html->url("/$slug") . "' class='btn btn-sm btn-success'>View</a>";
                        }
                        ?>
                        
                        <!--/nocache-->
                    </div>
                </div>
                <?php
            }
        }
        ?>
    </div>
</div>

<input type="hidden" id="org_remove_url" value="<?php echo $this->Html->url(array('controller' => 'organisation', 'action' => 'remove')); ?>" />

<script type="text/javascript">
(function() {
    var OrgList = View.extend(OrgList = {
        
        init: function() {
            this.bindBtns();
        },
        
        bindBtns: function() {
            var self = this;
            
            $(".org_remove").click(function(e) {
                e.preventDefault();
                
                var id = $(this).parents('.org_list_item').data('org-id');
                self.deleteOrganisation(id);
            });
        },
        
        deleteOrganisation: function(org_id) {
            var self = this;
            var confirm_msg = "This action will deactivate this organisation schedule it's data for deletion. Are you sure you want to continue?";
            
            confirm(confirm_msg, function() {
                alertify.log('Organisation removal process in progress. Please wait.');
                
                var url = $("#org_remove_url").val();
                var params = {organisation_id: org_id};
                
                $.ajax({
                    url: url,
                    data: params,
                    type: 'post',
                    dataType: 'json',
                    success: function(response) {
                        if( response.status == 'success' ) {
                            self.removeOrgListItem(org_id);
                            alert( response.message );
                        }
                        
                        else if( response.status == 'failed' ) {
                            alertify.error( response.message );
                        }
                    },
                    error: function(xhr) {
                        alertify.error("Organisation Deletion Operation Failed. Please try again later.");
                    }
                });
            });
        },
        
        removeOrgListItem: function(org_id) {
            $(".org_list_item").each(function() {
                if( $(this).data('org-id') == org_id ) {
                    $(this).hide('slow').remove();
                }
            });
        }
    });
    
    $(document).ready(function() {
        var ov = new OrgList();
    });
})();    
</script>

